<?php

//http://devzone.zend.com/node/view/id/687

class db
{
	private $link;
	private $config;
 
	function __construct($server=null, $user=null, $password=null, $dbname=null)
	{
		$this->config = array
		(
			'hostname'=>$server,
			'username'=>$user,
			'password'=>$password,
			'database'=>$dbname
		);
		
		$this->link = new mysqli(
			$this->config['hostname'],
			$this->config['username'],
			$this->config['password'],
			$this->config['database']
		);
	}
	
	function __destruct()
	{
		$this->disconnect();
	}
	
	function disconnect()
	{
		$this->link->close();
	}

	function execute($sql)
	{
		return $this->link->query($sql);
	}
	
	function numRows($sql)
	{
		$result = $this->link->query($sql);
		$numRows = $result->numRows();
		$result->close();
		return $numRows;
	}
	
	function numCols($sql)
	{
		$result = $this->link->query($sql);
		$numCols = $result->field_count();
		$result->close();
		return $numCols;
	}
	
	function getFields($sql)
	{
		$result = $this->link->query($sql);
		$getFields = $result->fetch_fields();
		$result->close();
		return $getFields;
	}

	function getObj($sql) //ahm?
	{
		$result = $this->link->query($sql);
		while($row = $result->fetch_object())
		{
			$rows[] = $row;
		}
		$result->close();
		return $rows;
	}

	function getAll($sql, $limit=null)
	{
//		krumo($sql);
		$result = $this->link->query($sql);
		while($row = $result->fetch_array(MYSQLI_ASSOC))
		{
			$rows[] = $row;
		}
		$result->close();
		return $rows;
	}

	function getOne($sql)
	{
		$result = $this->link->query($sql);
		$row = $result->fetch_array(MYSQLI_NUM);
		$result->close();
		return $row[0];
	}
	
	function getRow($sql)
	{
		$result = $this->link->query($sql);
		$getRow = $result->fetch_array(MYSQLI_ASSOC);
		$result->close();
		return $getRow;
	}
	
	function getCol($sql)
	{
	}
	
	function autoExecute($tableName, $data, $action)
	{
	}
	
	function autoInsert($tableName, $data)
	{
	}
	
	function autoUpdate($tableName, $data)
	{
	}
 
}
?>